package com.eascs.banknet.common.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.eascs.common.util.StringUtil;

public class CoreUtils {
	public static BigDecimal ZERO = new BigDecimal("0");

	public static BigDecimal ONEPERSENT = new BigDecimal("0.01");

	public static BigDecimal ONE = new BigDecimal("1");
	
	public static final String EMPTY = "";
	
	private static String ISO_DATE_FORMAT = "yyyy-MM-dd";

	private static String ISO_TIME_FORMAT = "yyyy-MM-dd hh:mm:ss";
	private static String ISO_YEARMONTH_FORMAT2 = "yyyy-MM";

	

	public static String formatDate(Date val) {

		if (val == null)
			return "";

		return DateFormatUtils.ISO_DATE_FORMAT.format(val);

	}

	public static String formatDate(Date date, String pattern) {
		try {
			return date != null ? DateFormatUtils.format(date, pattern) : null;
		} catch (Exception e) {
			return null;
		}
	}

	public static String formatDate(String dateString, String pattern) {
		return formatDate(parseDate(dateString), pattern);
	}

	public static String formatDatetime(Date val) {

		if (val == null)
			return "";

		return DateFormatUtils.ISO_DATETIME_FORMAT.format(val);

	}
	
	public static Date parseDate(String str) {

		if (StringUtils.isBlank(str))

			return null;

		try {

			return DateUtils.parseDate(str, new String[] { ISO_DATE_FORMAT });

		} catch (ParseException e) {

			return null;

		}

	}

	public static Date parseDate(String str, String format) {

		if (StringUtils.isBlank(str))

			return null;

		try {

			return DateUtils.parseDate(str, new String[] { format });

		} catch (ParseException e) {

			return null;

		}

	}
	
	public static String formatString(Object val) {
		if (null==val){
			return EMPTY;
		}

		if (val instanceof Date){
			return formatDate((Date) val);
		}

		return StringUtils.stripToEmpty(val.toString());

	}

	/**
	 * Long 类型转换，失败结果为NULL add by mching 2009-06-05
	 * 
	 * @param value
	 * @return Long
	 */
	public static Long parseLong(String value) {
		try {
			return StringUtil.isNotBlank(value) ? new Long(value) : null;
		} catch (Exception ex) {
			return null;
		}
	}
	

	public static String getTwoBitYear(Date val) {
		return formatDate(val).substring(2, 4);
	}

	public static String getFourBitYear(Date val) {
		return formatDate(val).substring(0, 4);
	}

	public static String getFourBitYearMonth(Date val) {
		String tmp = formatDate(val);
		return tmp.substring(2, 4) + tmp.substring(5, 7);
	}

	public static String getSixBitYearMonthDay(Date val) {
		String tmp = formatDate(val);
		return tmp.substring(2, 4) + tmp.substring(5, 7) + tmp.substring(8, 10);
	}

	public static String getEightBitYearMonthDay(Date val) {
		String tmp = formatDate(val);
		return tmp.substring(0, 4) + tmp.substring(5, 7) + tmp.substring(8, 10);
	}

	public static String getSixBitYearMonth(Date val) {
		String tmp = formatDate(val);
		return tmp.substring(0, 4) + tmp.substring(5, 7);
	}

	public static String getTwoBitYearMonth(Date val) {
		String tmp = formatDate(val);
		return tmp.substring(5, 7);
	}

	/**
	 * 生成指定长度的随机序列
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) {
		String letterchar = "1234567890abcdefghijklmnopqrstuvwxyz";
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			Random random = new Random();
			int t = random.nextInt(letterchar.length());
			sb.append(letterchar.charAt(t));
			try {
				Thread.sleep(15);
			} catch (InterruptedException e) {
			}
		}
		return sb.toString();
	}
}
